<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-03-10 23:28:10
 * @LastEditTime: 2023-10-23 10:14:00
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;


use Illuminate\Support\Facades\Storage;

class Table extends BaseModel
    {

    public $timestamps = true;
    public $table = "basedata_table";
    protected $guarded = [];
    protected $appends = ['excelurl'];
    protected $casts = [
        'showFooter'       => 'boolean',
        'import'           => 'boolean',
        'refresh'          => 'boolean',
        'search'           => 'boolean',
        'export'           => 'boolean',
        'zoom'             => 'boolean',
        'custom'           => 'boolean',
        'showColumnNum'    => 'boolean',
        'showCheckBox'     => 'boolean',
        'showOverflow'     => 'boolean',
        'just_fetch_field' => 'boolean',
        'tree'             => 'boolean',
        'print'            => 'boolean',
        'reload'           => 'boolean',
        'setting'          => 'boolean',

    ];
    public function columns()
        {
        return $this->hasMany(TableColumn::class)->orderBy("sort");
        }
    public function role_table()
        {
        return $this->hasMany(RoleTable::class, "table_id");
        }
    public function role()
        {
        return $this->hasManyThrough(Role::class, RoleTable::class, "table_id", "id", "id", "role_id");
        }
    public function getExcelurlAttribute()
        {
        return Storage::disk("reportexcel")->url($this->excelname);
        }
    public function buttons()
        {
        return $this->hasMany(TableButton::class)
            ->where("parent_id", 0)
            ->with("children")
            ->orderBy("sort");
        }

    public function allbuttons()
        {
        return $this->hasMany(TableButton::class);
        }
    public function forms()
        {
        return $this->hasMany(TableForm::class)->orderBy("sort");
        }
    public function reports()
        {
        return $this->hasMany(TableReport::class);
        }
    public function category()
        {
        return $this->belongsTo(Menu::class, "category_id");
        }
    }